package com.xiaou.controller;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.xiaou.entity.Users;

@Controller
public class UserController {
	
	@Autowired
	private Producer producer;
	
	@RequestMapping(value= {"/toLogin","/"})
	public String toLogin(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String msg = (String) session.getAttribute("msg");
		if(null != msg) {
			request.setAttribute("msg",msg);
			session.removeAttribute("msg");
		}
		
		return "login";
	}
	
	@RequestMapping("/index")
	public String index(Map<String, Object> map) {
		Users users = (Users) SecurityUtils.getSubject().getPrincipal();
		map.put("users", users);
		return "index";
	}
	
	@RequestMapping("/users/info")
	public String userInfo() {
		return "userInfo";
	}
	@RequestMapping("/unauthor")
	public String unauthor() {
		return "unauthor";
	}
	
	@RequestMapping("/login")
	public String Login(HttpServletRequest request,String username,String password,boolean rememberMe) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken userPasswordToken = new UsernamePasswordToken(username,password,rememberMe);
		HttpSession session = request.getSession();
		String vCode = (String) request.getAttribute("vCode");
		if(vCode != null) {
			session.setAttribute("msg", vCode);
			return "redirect:toLogin";
		}
		try {
			subject.login(userPasswordToken);
		}catch (ExcessiveAttemptsException e) {
			session.setAttribute("msg", e.getMessage());
			return "redirect:toLogin";
		}catch (AuthenticationException e) {
			session.setAttribute("msg", "用户名或密码错误");
			return "redirect:toLogin";
		}
		
		return "redirect:index";
	}	
	
	/**
	 * 验证码请求地址
	 * @return
	 */
	@RequestMapping("/captcha")
	public void captcha(HttpServletRequest req,HttpServletResponse rsp) {
		
		rsp.setDateHeader("Expires", 0);
	    // 设置游览器不缓存
		rsp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		rsp.addHeader("Cache-Control", "post-check=0, pre-check=0");
	    rsp.setHeader("Pragma", "no-cache");
	    // 设置返回类型
	    rsp.setContentType("image/jpeg");
	    
	    HttpSession session = req.getSession();
		String createText = producer.createText();
		session.setAttribute(Constants.KAPTCHA_SESSION_KEY,createText);
		ServletOutputStream out = null;
		try {
			out = rsp.getOutputStream();
			BufferedImage image = producer.createImage(createText);
			ImageIO.write(image,"jpg", out);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	    
	}
	
	
	@RequestMapping("users/userDel")
	@RequiresPermissions("users:delete")
	public String userDel() {
		return "userDel";
	}
	 
	
	
}
